﻿using System;
using System.Data;
using System.Collections;
using CE.Database;

/// <summary>
/// Summary description for MerchantCarPolicyDB
/// </summary>
namespace CE.CarInsurance
{
    public class MerchantCarPolicyDB
    {
        public Int64 Insert(MerchantCarPolicy merchantcarpolicy)
        {
            MyDatabase _db = new MyDatabase("GG_MCDBConnectionString");
            Int64 _result = -1;
            string _spName = "sprocMerchantCarPolicyInsertUpdateSingleItem";

            SortedList _sPara = new SortedList();

            #region Parameter
            _sPara.Add("@CarPolicyId", merchantcarpolicy.CECarPolicyId);
            _sPara.Add("@CarId", merchantcarpolicy.CECarId);
            _sPara.Add("@InsuranceType", merchantcarpolicy.InsuranceType);
            _sPara.Add("@GarageType", merchantcarpolicy.GarageType);
            _sPara.Add("@VehicleType", merchantcarpolicy.VehicleType);
            _sPara.Add("@SumInsured", merchantcarpolicy.SumInsured);
            _sPara.Add("@ProvinceRegistration", merchantcarpolicy.ProvinceRegistration);
            _sPara.Add("@UseOfVehicle", merchantcarpolicy.UseOfVehicle);
            _sPara.Add("@IsRedPlate", merchantcarpolicy.IsRedPlate);
            _sPara.Add("@IsAddAccMod", merchantcarpolicy.IsAddAccMod);
            _sPara.Add("@AddAccModAmount", merchantcarpolicy.AddAccModAmount);
            _sPara.Add("@CoverageCommenceDate", merchantcarpolicy.CoverageCommenceDate);
            _sPara.Add("@NCDPercent", merchantcarpolicy.NCDPercent);
            _sPara.Add("@IsClaimLastYear", merchantcarpolicy.IsClaimLastYear);
            _sPara.Add("@IsAddDriver", merchantcarpolicy.IsAddDriver);
            _sPara.Add("@NoOfDriver", merchantcarpolicy.NoOfDriver);
            _sPara.Add("@Deductible", merchantcarpolicy.Deductible);
            _sPara.Add("@IsCTPL", merchantcarpolicy.IsCTPL);
            _sPara.Add("@CurrentInsurer", merchantcarpolicy.CurrentInsurer);
            _sPara.Add("@CurrentStep", merchantcarpolicy.CurrentStep);
            _sPara.Add("@AgentId", merchantcarpolicy.AgentId);
            _sPara.Add("@RefCEUserId", merchantcarpolicy.RefCEUserId);
            _sPara.Add("@Status", merchantcarpolicy.Status);
            _sPara.Add("@NInfo1", merchantcarpolicy.NInfo1);
            _sPara.Add("@Info1", merchantcarpolicy.Info1);
            _sPara.Add("@Info2", merchantcarpolicy.Info2);
            _sPara.Add("@Info3", merchantcarpolicy.Info3);
            _sPara.Add("@Info4", merchantcarpolicy.Info4);
            _sPara.Add("@Info5", merchantcarpolicy.Info5);
            _sPara.Add("@Info6", merchantcarpolicy.Info6);
            _sPara.Add("@Info7", merchantcarpolicy.Info7);
            _sPara.Add("@Info8", merchantcarpolicy.Info8);
            _sPara.Add("@Info9", merchantcarpolicy.Info9);
            _sPara.Add("@Info10", merchantcarpolicy.Info10);
            _sPara.Add("@Info11", merchantcarpolicy.Info11);
            _sPara.Add("@Info12", merchantcarpolicy.Info12);
            _sPara.Add("@Info13", merchantcarpolicy.Info13);
            _sPara.Add("@Info14", merchantcarpolicy.Info14);
            _sPara.Add("@Info15", merchantcarpolicy.Info15);
            _sPara.Add("@Info16", merchantcarpolicy.Info16);
            _sPara.Add("@Info17", merchantcarpolicy.Info17);
            _sPara.Add("@Info18", merchantcarpolicy.Info18);
            _sPara.Add("@Info19", merchantcarpolicy.Info19);
            _sPara.Add("@Info20", merchantcarpolicy.Info20);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result;
        }

        public MerchantCarPolicy GetCarPolicy(Int64 CarId, Int64 CarPolicyId)
        {
            MerchantCarPolicy _merchantcarpolicy = null;
            MyDatabase _db = new MyDatabase("GG_MCDBConnectionString");
            string _spName = "sprocMerchantCarPolicySelectSingleItem";

            SortedList _spara = new SortedList();
            _spara.Add("@CECarId", CarId);
            _spara.Add("@CECarPolicyId", CarPolicyId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _merchantcarpolicy = new MerchantCarPolicy();
                    _merchantcarpolicy = FillDataRecord(_dt);
                }
            }
            return _merchantcarpolicy;
        }

        public MerchantCarPolicy GetCarPolicy(Int64 CarId)
        {
            MerchantCarPolicy _merchantcarpolicy = null;
            MyDatabase _db = new MyDatabase("GG_MCDBConnectionString");
            string _spName = "sprocMerchantCarPolicySelectSingleItem";

            SortedList _spara = new SortedList();
            _spara.Add("@CECarId", CarId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _merchantcarpolicy = new MerchantCarPolicy();
                    _merchantcarpolicy = FillDataRecord(_dt);
                }
            }
            return _merchantcarpolicy;
        }

        private MerchantCarPolicy FillDataRecord(DataTable myDataTable)
        {
            MerchantCarPolicy myPolicy = new MerchantCarPolicy();
            myPolicy.CECarPolicyId = Convert.ToInt64(myDataTable.Rows[0]["CECarPolicyId"].ToString());
            myPolicy.CECarId = Convert.ToInt32(myDataTable.Rows[0]["CECarId"].ToString());
            myPolicy.InsuranceType = Convert.ToInt32(myDataTable.Rows[0]["InsuranceType"].ToString());
            myPolicy.GarageType = Convert.ToInt32(myDataTable.Rows[0]["GarageType"].ToString());
            myPolicy.VehicleType = Convert.ToInt32(myDataTable.Rows[0]["VehicleType"].ToString());
            myPolicy.SumInsured = Convert.ToInt32(myDataTable.Rows[0]["SumInsured"].ToString());
            myPolicy.ProvinceRegistration = myDataTable.Rows[0]["ProvinceRegistration"].ToString();
            myPolicy.UseOfVehicle = myDataTable.Rows[0]["UseOfVehicle"].ToString();
            myPolicy.IsRedPlate = Convert.ToBoolean(myDataTable.Rows[0]["IsRedPlate"].ToString());
            myPolicy.IsAddAccMod = Convert.ToBoolean(myDataTable.Rows[0]["IsAddAccMod"].ToString());
            myPolicy.AddAccModAmount = Convert.ToInt32(myDataTable.Rows[0]["AddAccModAmount"].ToString());
            myPolicy.CoverageCommenceDate = Convert.ToDateTime(myDataTable.Rows[0]["CoverageCommenceDate"].ToString());
            myPolicy.NCDPercent = Convert.ToInt32(myDataTable.Rows[0]["NCDPercent"].ToString());
            myPolicy.IsClaimLastYear = Convert.ToBoolean(myDataTable.Rows[0]["IsClaimLastYear"].ToString());
            myPolicy.NoOfDriver = Convert.ToInt16(myDataTable.Rows[0]["NoOfDriver"].ToString());
            myPolicy.Deductible = Convert.ToInt16(myDataTable.Rows[0]["Deductible"].ToString());
            myPolicy.AgentId = Convert.ToInt64(myDataTable.Rows[0]["AgentId"].ToString());
            myPolicy.RefCEUserId = Convert.ToInt64(myDataTable.Rows[0]["RefCEUserId"].ToString());
            myPolicy.IsAddDriver = Convert.ToBoolean(myDataTable.Rows[0]["IsAddDriver"].ToString());
            myPolicy.IsCTPL = Convert.ToBoolean(myDataTable.Rows[0]["IsCTPL"].ToString());
            myPolicy.CurrentInsurer = myDataTable.Rows[0]["CurrentInsurer"].ToString();
            myPolicy.Status = Convert.ToBoolean(myDataTable.Rows[0]["Status"].ToString());
            myPolicy.NInfo1 = Convert.ToInt64(myDataTable.Rows[0]["NInfo1"].ToString());

            if (myDataTable.Rows[0]["Info6"] != DBNull.Value)
            {
                myPolicy.Info6 = myDataTable.Rows[0]["Info6"].ToString();
            }

            if (myDataTable.Rows[0]["Info7"] != DBNull.Value)
            {
                myPolicy.Info7 = myDataTable.Rows[0]["Info7"].ToString();
            }

            if (myDataTable.Rows[0]["Info8"] != DBNull.Value)
            {
                myPolicy.Info8 = myDataTable.Rows[0]["Info8"].ToString();
            }
            return myPolicy;
        }

    }
}
